# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
import os
from py2neo import Graph,Node,Relationship
import json
import configparser
from . import login_required
cf = configparser.ConfigParser()
cf.read("./kg.conf")  # 读取配置文件，如果写文件的绝对路径，就可以不用os模块



#print(neo4j_url,neo4j_username,neo4j_password)


def search_entity(request):
    ctx = {}

    return render(request,"graph_math.html",{'ctx':ctx})

def search_relation(request):
    ctx = {}

    return render(request,'relation.html',{'ctx':ctx}) 


@csrf_exempt
def get_case_list(request):
    res = {}
    data=[]
    filepath = cf.get("public", "caselistpath") 
    if os.path.exists(filepath)==True: 
        freadcaselist = open(filepath,"r")  
        lines = freadcaselist.readlines()#读取全部内容  
        for line in lines:
            line=line.strip('\n')
            linesplit=line.split('\t')
            if len(linesplit)==4:
                casemap={}
                casemap["potentialreasons"]=linesplit[0]
                casemap["potentialresult"]=linesplit[1]
                casemap["souceurl"]=linesplit[2]
                casemap["inputupdate"]=linesplit[3]
                data.append(casemap)
             
    n = len(data)
    res = {
        "code": 0,
        "msg": ",",
        "count": n,
        "data": data
    }
    #print("res",res)
    return HttpResponse(json.dumps(res), content_type="application/json")
@csrf_exempt
def do_db_labels_types(request):
    #call db.labels
    #call db.relationshipTypes
    neo4j_url = cf.get("neo4j", "url")  
    neo4j_username = cf.get("neo4j", "username")  
    neo4j_password = cf.get("neo4j", "password")  
    graph = Graph(neo4j_url,auth=(neo4j_username,neo4j_password))
    labelsret=graph.run("call db.labels").data() 
    #print(labelsret)
    relationshiptypesret=graph.run("call db.relationshipTypes").data() 
    #print(relationshiptypesret)

    res = {}
    data=[]
    data.append({"cont": '<a>【实体类型】<br> </a>'})
    for  label in labelsret:
        contstr='<a href="javascript:void(0);" onclick="runlab(this)"">'+label['label']+'&nbsp;&nbsp;&nbsp;&nbsp;</a>'
        data.append({"cont": contstr})
    data.append({"cont": '<a><br>【关系类型】<br> </a>'})
    for  rel in relationshiptypesret:
        contstr='<a href="javascript:void(0);" onclick="runrel(this)"">'+rel['relationshipType']+'&nbsp;&nbsp;&nbsp;&nbsp;</a>'
        data.append({"cont": contstr})
    n = len(data)
    #print(data)
    #print(n)
    res = {"code": 0, "msg": ",","count": n,"data": data }
    return HttpResponse(json.dumps(res), content_type="application/json")

@csrf_exempt
def getgrapinfo(request):
    bolt_url = cf.get("bolt", "url")  
    bolt_username = cf.get("bolt", "username")  
    bolt_password = cf.get("bolt", "password")  
    grapinfo = {
            'server_url': bolt_url,
            'server_user': bolt_username,
            'server_password': bolt_password
    }
    return HttpResponse(json.dumps(grapinfo), content_type="application/json")

@csrf_exempt
def getcasedetail(request):
    filename=request.GET.get('filepath')
    filedir = cf.get("public", "casedetaildir") 
    filepath= filedir+"/"+filename
    print(filepath)
    casecont=''
    if os.path.exists(filepath)==True:
        readcasef = open(filepath, 'r')
        casecont=readcasef.read()
        readcasef.close()
    else:
        casecont='抱歉~原始文件丢失'
    
    casecont=casecont.replace("\n","</br>")
    casecont=casecont.replace(" ","&nbsp;")
    casedetail = {
            'cont': casecont
    }
    return HttpResponse(json.dumps(casedetail), content_type="application/json")

@csrf_exempt
def t1_test(request):  # 
	context = {}
	return render(request, 'test1.html', context)

@csrf_exempt
def apply(request):  # 
	context = {}
	return render(request, 'apply.html', context)




@csrf_exempt
def get_apply_list(request):
    res = {}
    data=[]
    filepath = cf.get("public", "applylistpath") 
    if os.path.exists(filepath)==True: 
        freadcaselist = open(filepath,"r")  
        lines = freadcaselist.readlines()#读取全部内容  
        for line in lines:
            line=line.strip('\n')
            linesplit=line.split('\t')
            if len(linesplit)==4:
                applymap={}
                applymap["applytype"]=linesplit[0]
                applymap["applyid"]=linesplit[1]
                applymap["applydetail"]=linesplit[2]
                applymap["inputupdate"]=linesplit[3]
                data.append(applymap)
             
    n = len(data)
    res = {
        "code": 0,
        "msg": ",",
        "count": n,
        "data": data
    }
    #print("res",res)
    return HttpResponse(json.dumps(res), content_type="application/json")


@csrf_exempt
def applydetailsouceurl(request):
    filename=request.GET.get('filepath')
    filedir = cf.get("public", "applydetaildir") 
    filepath= filedir+"/"+filename
    print(filepath)
    applycont=''
    if os.path.exists(filepath)==True:
        readcasef = open(filepath, 'r')
        applycont=readcasef.read()
        readcasef.close()
    else:
        applycont='抱歉~原始文件丢失'
    
    applycont=applycont.replace("\n","</br>")
    applycont=applycont.replace(" ","&nbsp;")
    applydetail = {
            'cont': applycont
    }
    return HttpResponse(json.dumps(applydetail), content_type="application/json")


@csrf_exempt
def get_collect_list(request):
    res = {}
    data=[]
    filepath = cf.get("public", "collectlistpath") 
    if os.path.exists(filepath)==True: 
        freadcaselist = open(filepath,"r")  
        lines = freadcaselist.readlines()#读取全部内容  
        for line in lines:
            line=line.strip('\n')
            linesplit=line.split('\t')
            if len(linesplit)==3:
                collectmap={}
                collectmap["collecttype"]=linesplit[0]
                collectmap["collectcont"]=linesplit[1]
                collectmap["inputupdate"]=linesplit[2]
                data.append(collectmap)
             
    n = len(data)
    res = {
        "code": 0,
        "msg": ",",
        "count": n,
        "data": data
    }
    #print("res",res)
    return HttpResponse(json.dumps(res), content_type="application/json")
